<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>
<script src="../resources/common.js"></script>
<script src="../resources/picker-common.js"></script>
<script src="../calendar-picker/resources/calendar-picker-common.js"></script>
</head>
<body>
<input type="week" id="week-0" value="2019-W17" max="2019-W21">
<input type="week" id="week-1" value="2019-W20" min="2019-W17">
<input type="week" id="week-2" value="2019-W25" step="3">
<input type="week" id="week-3" value="2020-W12" step="3">
<script>

promise_test(() => {
  let weekElement = document.getElementById('week-0');
  return openPickerWithPromise(weekElement)
  .then(() => {
    // Make the picker dismiss synchronously so we don't need to insert
    // an artificial delay in the test
    internals.pagePopupWindow.CalendarPicker.commitDelayMs = 0;

    clickNextMonthButton();
    assert_equals(weekElement.value, "2019-W21", "Month chooser button should have changed month and stopped at max week");

    clickDayCellAt(1, 2);
    assert_equals(weekElement.value, "2019-W20", "Clicking week should have changed week");
    assert_equals(internals.pagePopupWindow, null, "Clicking week should have dismissed popup");
  });
}, "Week picker: Next month button should advance to next month and clip selection to max week.");

promise_test(() => {
  let weekElement = document.getElementById('week-1');
  return openPickerWithPromise(weekElement)
  .then(() => {
    // Make the picker dismiss synchronously so we don't need to insert
    // an artificial delay in the test
    internals.pagePopupWindow.CalendarPicker.commitDelayMs = 0;

    clickPrevMonthButton();
    assert_equals(weekElement.value, "2019-W17", "Month chooser button should have changed month and stopped at min week");

    clickDayCellAt(1, 4);
    assert_equals(weekElement.value, "2019-W18", "Clicking week should have changed week");
    assert_equals(internals.pagePopupWindow, null, "Clicking week should have dismissed popup");
  });
}, "Week picker: Previous month button should advance to previous month and clip selection to min week.");

promise_test(() => {
  let weekElement = document.getElementById('week-2');
  return openPickerWithPromise(weekElement)
  .then(() => {
    // Make the picker dismiss synchronously so we don't need to insert
    // an artificial delay in the test
    internals.pagePopupWindow.CalendarPicker.commitDelayMs = 0;

    clickNextMonthButton();
    assert_equals(weekElement.value, "2019-W28", "Closest valid week to 2019-W29 should have been selected");

    clickDayCellAt(1, 4);
    assert_equals(weekElement.value, "2019-W31", "Clicking week should have changed week");
    assert_equals(internals.pagePopupWindow, null, "Clicking week should have dismissed popup");
  });
}, "Week picker: If same week in next month is invalid, next month button should choose closest value -- smaller week case");

promise_test(() => {
  let weekElement = document.getElementById('week-3');
  return openPickerWithPromise(weekElement)
  .then(() => {
    // Make the picker dismiss synchronously so we don't need to insert
    // an artificial delay in the test
    internals.pagePopupWindow.CalendarPicker.commitDelayMs = 0;

    clickNextMonthButton();
    assert_equals(weekElement.value, "2020-W18", "Closest valid week to 2020-W17 should have been selected");

    clickDayCellAt(1, 1);
    assert_equals(weekElement.value, "2020-W15", "Clicking week should have changed week");
    assert_equals(internals.pagePopupWindow, null, "Clicking week should have dismissed popup");
  });
}, "Week picker: If same week in next month is invalid, next month button should choose closest value -- larger week case");

</script>
</body>
</html>
